	//
//  HomeViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/16.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "HomeViewController.h"
#import "UIImage+Addition.h"
#import "HomeHotCollectionViewCell.h"

#import "HomeBannerOneCollectionViewCell.h"
#import "ProductInfoViewController.h"
#import "AuthorizationSearchVc.h"


static NSString *kCellOne = @"HomeBannerOneCollectionViewCell";
static NSString *kCellTwo = @"HomeHotCollectionViewCell";
static NSString *kCellHead = @"HomeHotCollectionReusableView";


typedef enum : NSUInteger {
    /**搜索时的背景viewTag*/
    kViewTagBgViewTag=1024,
} ViewTag;

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
{

    int _page;
    
}
@property (nonatomic, strong) UICollectionView *homeCollectionView;
@property(nonatomic, strong) NSMutableArray * hotSellGoodsArr;

@end


@implementation HomeViewController
- (NSMutableArray *)hotSellGoodsArr{

    if (_hotSellGoodsArr==nil) {
        _hotSellGoodsArr = [NSMutableArray array];
    }
    return _hotSellGoodsArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = Color_Back_Black;
  
    [self UI];
    [self NaviUI];
    _page = 1;
    [self getHotSellGoodsListIsLoadMore:NO];
}

//- (void)viewWillAppear:(BOOL)animated {
//    
//    [((TabBarController *)self.tabBarController) setTabbarHidden:NO];
//}

- (void)UI {
    //布局方式
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    CGFloat wide = (IOS_IPHONE_WINDOW_WIDE - 12 - 14 * 2) / 2;//流布局的宽度
    CGFloat high = wide * 191.0f / 141.0f;//流布局的高度
    flowLayOut.itemSize = CGSizeMake(wide, high);
    flowLayOut.sectionInset = UIEdgeInsetsMake(14, 14, 0, 14);
    CGRect frame = self.view.bounds;
    frame.size.height -= 64;
    self.homeCollectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayOut];
    self.homeCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.homeCollectionView registerNib:[UINib nibWithNibName:kCellTwo bundle:nil] forCellWithReuseIdentifier:kCellTwo];
    [self.homeCollectionView registerClass:[HomeBannerOneCollectionViewCell class] forCellWithReuseIdentifier:kCellOne];
    [self.homeCollectionView registerNib:[UINib nibWithNibName:kCellHead bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCellHead];
    [self.view addSubview:self.homeCollectionView];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
//    self.title = @"名小将的旅行箱";
    UILabel * natigationItemLabel = [[UILabel alloc] init];
    natigationItemLabel.font = [UIFont boldSystemFontOfSize:16];
    natigationItemLabel.textColor = [UIColor whiteColor];
    natigationItemLabel.frame = CGRectMake(0, 0, 90, 40);
    natigationItemLabel.textAlignment = NSTextAlignmentCenter;
//    natigationItemLabel
    natigationItemLabel.text = @"将行";
    self.navigationItem.titleView = natigationItemLabel;
    
    [self.rightBtn setTitle:@"授权查询" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];



}

- (void)getHotSellGoodsListIsLoadMore:(BOOL)isLoadMore{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"pageSize"] = pageSize;
    param[@"type"]= @(0);
    if (isLoadMore) {
        if (self.hotSellGoodsArr.count==0) {
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"没有更多商品"];
            return;
        }else{
        
            ++_page;
        }
    }else{
    
        _page = 1;
    }
    param[@"page"] = @(_page);
    param[@"code"] = @"";
    param[@"name"] = @"";
    param[@"size"] = @(0);
    param[@"color_id"] = @(0);
    param[@"is_lock"] = @(-1);
    param[@"order_price"] = @"other";
    param[@"order_time"] = @"other";
    param[@"sale_num"] = @"other";
    param[@"pageSize"] = @(20);
    [WLHTTPRequest getCommodityListWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        NSLog(@"%@",resultDictionary);
        [self.hotSellGoodsArr addObjectsFromArray:resultDictionary[@"data"]];
        [self.homeCollectionView reloadData];
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)NaviUI {
    
////    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 33, 19)];
    UINavigationBar * appearce = [UINavigationBar appearance];
//    self.leftBtn.frame = CGRectMake(0, 0, 33, 19);
    [self.leftBtn setBackgroundImage:LOAD_IMAGE(@"搜索@2x.png") forState:UIControlStateNormal];
////    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftBtn] ;
    
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:20],NSFontAttributeName ,nil];
    [self.navigationController.navigationBar setTitleTextAttributes:textDic];
    
    [appearce setTitleTextAttributes:textDic];
//
    [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:Color_Back_Black] forBarMetrics:UIBarMetricsDefault];
    [appearce setBackgroundImage:[UIImage createImageWithColor:Color_Back_Black] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;//去掉透明度

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    return self.hotSellGoodsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        HomeBannerOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellOne forIndexPath:indexPath];
        
        return cell;
        
    }else {
        
        HomeHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellTwo forIndexPath:indexPath];
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = RGB(215, 215, 215).CGColor;
        cell.infoDic = self.hotSellGoodsArr[indexPath.row];
        return cell;
    }
    

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    

    NSString *reuseIdentifier = kCellHead;
    UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind
                                                                          withReuseIdentifier:reuseIdentifier
                                                                                 forIndexPath:indexPath];

    return view;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        CGFloat wide1 = IOS_IPHONE_WINDOW_WIDE - 10 * 2;
        CGFloat high1 = wide1 *  395.0f / 605.0f;
        CGSize size1 = {wide1,high1};
        return size1;
    }
    CGFloat wide = (IOS_IPHONE_WINDOW_WIDE - 12 - 14 * 2) / 2;
    CGFloat high = wide * 191.0f / 141.0f;
    CGSize size = {wide,high};
    return size;
}

//返回头headerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return CGSizeZero;
    }
    CGSize size={20,35};
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"id"] = self.hotSellGoodsArr[indexPath.row][@"id"];
    [WLHTTPRequest getGoodsDeatailWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        NSLog(@"商品详情:%@",resultDictionary);
        ProductInfoViewController * product = [[ProductInfoViewController alloc] init];
        product.detailDic = resultDictionary[@"data"];
         [self.navigationController pushViewController:product animated:YES];
    } andFailureBlock:^(NSError *resultError) {
        
    }];
   
}
- (void)back{
    self.navigationItem.titleView = nil;
    UIView * bgView = [[UIView alloc] initWithFrame:self.view.bounds];
    bgView.tag = kViewTagBgViewTag;
    bgView.alpha = 0.8;
    bgView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bgView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRightButton)];
    [bgView addGestureRecognizer:tap];
    
    UITextField * searchField =[[UITextField alloc] initWithFrame:CGRectMake(0, 0, IOS_IPHONE_WINDOW_WIDE-80, 30)];
    searchField.placeholder = @"搜索商品";
    searchField.returnKeyType = UIReturnKeySearch;
    searchField.delegate = self;
//    searchField.leftView = [UIImageView alloc] initWithImage:LOAD_IMAGE(@"");
    searchField.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.titleView = nil;
    [searchField becomeFirstResponder];

    [self.rightBtn setTitle:@"取消" forState: UIControlStateNormal];
    self.rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:searchField];
 
  
}
- (void)clickRightButton{

    if ([self.rightBtn.titleLabel.text isEqualToString:@"取消"]) {
        [[self.view viewWithTag:kViewTagBgViewTag] removeFromSuperview];
        UILabel * natigationItemLabel = [[UILabel alloc] init];
        natigationItemLabel.font = [UIFont boldSystemFontOfSize:16];
        natigationItemLabel.textColor = [UIColor whiteColor];
        natigationItemLabel.frame = CGRectMake(0, 0, 90, 40);
        natigationItemLabel.textAlignment = NSTextAlignmentCenter;
        //    natigationItemLabel
        natigationItemLabel.text = @"将行";
        self.navigationItem.titleView = natigationItemLabel;
        
        [self.rightBtn setTitle:@"授权查询" forState:UIControlStateNormal];
        [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftBtn];
    }else{
        AuthorizationSearchVc * authVc = [[AuthorizationSearchVc
                                           alloc] init];
        [self.navigationController pushViewController:authVc animated:YES];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSLog(@"点击了搜索");
    [self clickRightButton];
    return YES;
}
@end
